//
//  MyjifenViewController.m
//  LoveAttention
//
//  Created by 鞠超 on 15/12/29.
//  Copyright © 2015年 JC. All rights reserved.
//

#import "MyjifenViewController.h"
#import "DataProvider.h"
#import "SVProgressHUD.h"
#import "MyjifenTableViewCell.h"
#import "Model_Myjifen.h"

#import "MJRefresh.h"
@interface MyjifenViewController () <UITableViewDelegate, UITableViewDataSource>
//积分
@property (nonatomic, strong) UILabel * label_jifen;
//邀请
@property (nonatomic, strong) UILabel * label_yaoqing;
//消费
@property (nonatomic, strong) UILabel * label_xiaofei;
//提现
@property (nonatomic, strong) UILabel * label_tixian;

@property (nonatomic, strong) UITableView * tableView;

@property (nonatomic, strong) NSMutableArray * arr_data;

@property (nonatomic, assign) NSInteger  page;
@end

@implementation MyjifenViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self p_navigationController];
    
    [self p_data];
    
    [self p_data2];
    
    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - navigationController
- (void)p_navigationController
{
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    self.navigationItem.title = @"我的积分";
    UIButton * btn = [UIButton buttonWithType:(UIButtonTypeSystem)];
    [btn setBackgroundImage:[UIImage imageNamed:@"返回"] forState:(UIControlStateNormal)];
        btn.frame = CGRectMake(10, 20, 25, 30);
    [btn addTarget:self action:@selector(leftBarButtonItemAction:) forControlEvents:(UIControlEventTouchUpInside)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
}
//点击事件(左)
- (void)leftBarButtonItemAction:(UIBarButtonItem *)bar
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 布局
- (void)p_setupView
{
    
    
    UIView * view_1 = [[UIView alloc] initWithFrame:CGRectMake(0, 64 + 5, self.view.frame.size.width, 55)];
    view_1.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view_1];
    
    CGFloat x_width = self.view.frame.size.width/3;
    UILabel * label_1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 15, x_width, 30)];
    label_1.text = @"当前积分";
    label_1.font = [UIFont systemFontOfSize:13];
    label_1.textAlignment = NSTextAlignmentCenter;
    [view_1 addSubview:label_1];
    
    UILabel * label_2 = [[UILabel alloc] initWithFrame:CGRectMake( CGRectGetMaxX(label_1.frame), 15, x_width, 30)];
    label_2.text = @"邀请返佣";
    label_2.font = [UIFont systemFontOfSize:13];
    label_2.textAlignment = NSTextAlignmentCenter;
    [view_1 addSubview:label_2];
    
    UILabel * label_3 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(label_2.frame), 15, x_width, 30)];
    label_3.text = @"消费返佣";
    label_3.font = [UIFont systemFontOfSize:13];
    label_3.textAlignment = NSTextAlignmentCenter;
    [view_1 addSubview:label_3];
    
    UILabel * label_4 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(label_3.frame), 15, x_width, 30)];
    label_4.text = @"提现";
    label_4.font = [UIFont systemFontOfSize:13];
    label_4.textAlignment = NSTextAlignmentCenter;
//    [view_1 addSubview:label_4];
    
    
    UIView * view_2 = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(view_1.frame) + 1, self.view.frame.size.width, 55)];
    view_2.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view_2];
    
    UILabel * label_detail = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(view_2.frame) + 5, self.view.frame.size.width, 20)];
    label_detail.text = @"积 分 详 情";
    label_detail.font = [UIFont systemFontOfSize:13];
    label_detail.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:label_detail];
    
    
    self.label_jifen = [[UILabel alloc] initWithFrame:CGRectMake(0, 15, x_width, 30)];
//    self.label_jifen.text = @"1000";
    self.label_jifen.font = [UIFont systemFontOfSize:13];
    self.label_jifen.textAlignment = NSTextAlignmentCenter;
    [view_2 addSubview:self.label_jifen];
    
    self.label_yaoqing = [[UILabel alloc] initWithFrame:CGRectMake( CGRectGetMaxX(label_1.frame), 15, x_width, 30)];
//    self.label_yaoqing.text = @"1000";
    self.label_yaoqing.font = [UIFont systemFontOfSize:13];
    self.label_yaoqing.textAlignment = NSTextAlignmentCenter;
//    self.label_yaoqing.backgroundColor = [UIColor orangeColor];
    [view_2 addSubview:self.label_yaoqing];
    
    self.label_xiaofei = [[UILabel alloc] initWithFrame:CGRectMake( CGRectGetMaxX(label_2.frame), 15, x_width, 30)];
//    self.label_xiaofei.text = @"1000";
    self.label_xiaofei.font = [UIFont systemFontOfSize:13];
    self.label_xiaofei.textAlignment = NSTextAlignmentCenter;
    [view_2 addSubview:self.label_xiaofei];
    
    self.label_tixian = [[UILabel alloc] initWithFrame:CGRectMake( CGRectGetMaxX(label_3.frame), 15, x_width, 30)];
//    self.label_tixian.text = @"1000";
    self.label_tixian.font = [UIFont systemFontOfSize:13];
    self.label_tixian.textAlignment = NSTextAlignmentCenter;
//    [view_2 addSubview:self.label_tixian];
    
    self.automaticallyAdjustsScrollViewInsets = YES;
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(view_2.frame) + 25, self.view.frame.size.width, self.view.frame.size.height - CGRectGetMaxY(view_2.frame) - 15) style:(UITableViewStylePlain)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.tableView.tableFooterView = [[UIView alloc] init];
    
    [self.view addSubview:self.tableView];
    
    [self.tableView registerClass:[MyjifenTableViewCell class] forCellReuseIdentifier:@"cell_Myjifen"];
    
    [self example01];
    [self example02];
}

#pragma mark - tableView
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr_data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Model_Myjifen * model = self.arr_data[indexPath.row];
    
    MyjifenTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell_Myjifen" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.label_1.text = model.Infovalue;
    cell.label_2.text = model.exchang;
    cell.label_3.text = model.timeValue;
//    cell.label_4.text = model.outvalue;
    
    
    return cell;
}

- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

#pragma mark - 接口
- (void)p_data
{
    DataProvider * dataprovider = [[DataProvider alloc] init];
    
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [dataprovider setDelegateObject:self setBackFunctionName:@"MyJinfenCall:"];
//    [dataprovider myJifenWith:[defaults objectForKey:@"categoryid"]];

    [dataprovider myJifenWith:[defaults objectForKey:@"merchantid"]];
}

- (void)MyJinfenCall:(id )dict
{
//    NSLog(@"%@",dict);
    if ([dict[@"status"][@"succeed"] intValue]==1) {
        @try
        {
            self.label_jifen.text = [NSString stringWithFormat:@"%@",dict[@"data"][@"curvalue"]];
            self.label_yaoqing.text = [NSString stringWithFormat:@"%@",dict[@"data"][@"yaovalue"]];
            self.label_xiaofei.text = [NSString stringWithFormat:@"%@",dict[@"data"][@"payvalue"]];
//            self.label_tixian.text = dict[@"data"][@"remainvalue"];
        }
        @catch (NSException *exception)
        {
            NSLog(@"保存userdefault出错");
        }
        @finally {
            
        }
    }
    else
    {
        [SVProgressHUD showErrorWithStatus:dict[@"status"][@"errdesc"] maskType:SVProgressHUDMaskTypeBlack];
    }

}

#pragma mark - 数据2
- (void)p_data2
{
    DataProvider * dataprovider = [[DataProvider alloc] init];
    
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    
    [dataprovider setDelegateObject:self setBackFunctionName:@"jifenChange:"];
    
    [dataprovider jifenChangeWith:[defaults objectForKey:@"merchantid"] andpagenumber:@"1" andpagesize:@"15"];
}

- (void)jifenChange:(id )dict
{
    NSLog(@"%@",dict);
    
    self.arr_data = nil;
    
    if ([dict[@"status"][@"succeed"] intValue]==1) {
        @try
        {
            for (NSDictionary * dic in dict[@"data"][@"detail"]) {
                
                Model_Myjifen * model = [[Model_Myjifen alloc] init];
                
                [model SetValuesWithDictionary:dic];
                
                [self.arr_data addObject:model];
            }
        }
        @catch (NSException *exception)
        {
            
        }
        @finally {
            
            
            [self.tableView reloadData];
        }
    }
    else
    {
        [SVProgressHUD showErrorWithStatus:dict[@"status"][@"errdesc"] maskType:SVProgressHUDMaskTypeBlack];
    }

}

- (NSMutableArray *)arr_data
{
    if(_arr_data == nil)
    {
        self.arr_data = [NSMutableArray array];
    }
    return _arr_data;
}

- (void)p_data3:(NSString *)page
{
    DataProvider * dataprovider = [[DataProvider alloc] init];
    
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    
    [dataprovider setDelegateObject:self setBackFunctionName:@"jifenChange1:"];
    
    [dataprovider jifenChangeWith:[defaults objectForKey:@"merchantid"] andpagenumber:page andpagesize:@"15"];
}

- (void)jifenChange1:(id )dict
{
    NSLog(@"%@",dict);
    
    if ([dict[@"status"][@"succeed"] intValue]==1) {
        @try
        {
            for (NSDictionary * dic in dict[@"data"]) {
                
                Model_Myjifen * model = [[Model_Myjifen alloc] init];
                
                [model setValuesForKeysWithDictionary:dic];
                
                [self.arr_data addObject:model];
            }
        }
        @catch (NSException *exception)
        {
            
        }
        @finally {
            
            
            [self.tableView reloadData];
        }
    }
    else
    {
        [SVProgressHUD showErrorWithStatus:dict[@"status"][@"errdesc"] maskType:SVProgressHUDMaskTypeBlack];
    }
    
}


#pragma mark - 下拉刷新
- (void)example01
{
    __weak __typeof(self) weakSelf = self;
    
    // 设置回调（一旦进入刷新状态就会调用这个refreshingBlock）
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [weakSelf p_data2];
        
        [weakSelf loadNewData];
        
        self.page = 1;
        
    }];
    // 马上进入刷新状态
    [self.tableView.mj_header beginRefreshing];
}

-(void)example02
{
    __weak __typeof(self) weakSelf = self;
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        self.page ++;
        
        [weakSelf loadNewData];
        
        [weakSelf p_data3:[NSString stringWithFormat:@"%ld",self.page]];
    }];
    
}

- (void)loadNewData
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    });
    
}



@end
